<?php

class PageBase implements IController {
    public $Smarty;
    public $BaseDir;
    public $Controller;
    public $Language;
    public $Id;
    private $ControllerName;
    private $Action;
    private $DefaultController = "Index";
    private $DefaultAction = "Index";


    function __construct($baseDir = null, $debugging = false){
/*
        TestDataGeneration::GenerateEnums();
        TestDataGeneration::GenerateParty();
        TestDataGeneration::GenerateObjects();
        TestDataGeneration::GenerateTranslations();
*/
        $this->DetermineController($baseDir);

        $this->Smarty = new SmartyDoctrine($this->BaseDir, $this->Language, $debugging);
        $this->SetDataToSmarty();

        $this->LoadCtrl();
    }

    //region page life cycle

    public function OnLoad(){
        $this->Controller->OnLoad();
    }

    public function OnInit(){
        $this->Controller->OnInit();
    }

    public function OnRender(){
        $this->Controller->OnRender();
    }

    public function Execute(){
        $this->Controller->Execute();
    }

    //endregion page life cycle

    private function SetDataToSmarty(){
        $this->Smarty->assign("baseDir", $this->BaseDir);
        $this->Smarty->assign("controllerName", $this->ControllerName);
        $this->Smarty->assign("action", $this->Action);
        $this->Smarty->assign("language", $this->Language);
        $this->Smarty->assign("languages", appcode\enum\Language::GetArray());
        $this->Smarty->assign("id", $this->Id);

        $css = "@import url('/CodeVault/content/styles/version1/base.css');\n";
        $css .= "@import url('/CodeVault/content/styles/version1/navigation.css');\n";
        $css .= "@import url('/CodeVault/content/styles/version1/header.css');\n";
        $css .= "@import url('/CodeVault/content/styles/version1/breadcrumbs.css');\n";
        $css .= "@import url('/CodeVault/content/styles/version1/content.css');\n";
        $css .= "@import url('/CodeVault/content/styles/version1/message.css');\n";
        $css .= "@import url('/CodeVault/content/styles/version1/footer.css');\n";
        $css .= "@import url('/CodeVault/content/styles/version1/form.css');\n";
        $css .= "@import url('/CodeVault/content/styles/version1/pager.css');\n";
        $this->Smarty->assign("cssBase", $css);

        if($this->Smarty->debugging){
            $debugInfo["BaseDir"] = $this->BaseDir;
            $debugInfo["ControllerName"] = $this->ControllerName;
            $debugInfo["Action"] = $this->Action;
            $debugInfo["Language"] = $this->Language;
            $debugInfo["Id"] = $this->Id;
            $di["PAGE_BASE_DEBUG_INFO"][] = $debugInfo;
            echo "<pre>";
            print_r($di);
            echo "</pre>";
        }
    }

    private function DetermineController($baseDir = null){
        $req = array_slice(explode('/', $_SERVER['REQUEST_URI']), 1);
        $base_count = empty($baseDir) ? 0 : count(explode('/', $baseDir));

        $language = $this->GetRequestItem($req, $base_count);
        $controller = $this->GetRequestItem($req, $base_count, 1);
        $action = $this->GetRequestItem($req, $base_count, 2);
        $id = $this->GetRequestItem($req, $base_count, 3);

        if(empty($controller)){
            $controller = $this->DefaultController;
        }

        if(empty($action)){
            $action = $this->DefaultAction;
        }

        $this->BaseDir = $baseDir;
        $this->ControllerName = ucfirst(strtolower($controller));
        $this->Action = ucfirst(strtolower($action));
        $this->Language = $this->ValidateLanguage(strtolower($language));
        $this->Id = $id;
    }

    private function GetRequestItem($req, $base_count, $index = 0){
        $key = (isset($req[$base_count + $index]) ? $req[$base_count + $index] : null);
        $val = explode('?', $key);
        return $val[0];
    }

    private function ValidateLanguage($selected = null){
        $languages = appcode\enum\Language::GetArray();
        $isExisting = false;
        foreach ($languages as $lang) {
            if($selected == $lang){
                $isExisting = true;
                break;
            }
        }
        if(!$isExisting){
            $selected = null;
        }
        if (empty($selected)){
            return $languages[1];
        }
        return $selected;
    }

    private function GetFilePath($className, $dir = null){
        $isDefaultDir = !isset($dir);

        $dir = $isDefaultDir ? "./controllers/" : $dir ;

        $file_name = $dir.$className.".php";

        if (file_exists($file_name) && is_file($file_name)) {
            include_once($file_name);
        }
        else if($isDefaultDir){
            echo "Controller '".$this->ControllerName."' does not exist!";
            Exit;
        }
        else{
            $this->GetFilePath($className);
        }
    }

    private function LoadCtrl(){
        $className = $this->ControllerName."Ctrl";

        if(!class_exists($className)){
            $this->GetFilePath($className, defined('BASE_CONTROLLERS_DIR') ? BASE_CONTROLLERS_DIR : null);

            $this->Controller = new $className($this->Smarty, $this->BaseDir, $this->ControllerName, $this->Action, $this->Language, $this->Id);

            if(!method_exists($this->Controller, $this->Action)){
                echo "Action '".$this->Action."' does not exist for control '".$this->ControllerName."'!";
                Exit;
            }
        }
    }
}